// 登录、注册、忘记密码页面板块
import {
    login,
    systemMessage,
    changePassword
} from "../../api/access.js";

import Cookie from 'js-cookie'

const state = () => ({
    token: Cookie.get("token")===undefined?'':Cookie.get("token"), // 登录之后获取的token
    adminId: '', // 学院管理员的id
    account: '', // 管理员账号
    identity: '',
    collegeId: Cookie.get("collegeId")===undefined?'':Cookie.get("collegeId"),//存储管理员的学院ID
    adminName:'',//存储管理员的名字
    collegeName:'',//存储管理员所在学院
    systemMessage: {
        studentCount: 0,
        teacherCount: 0,
        courseCount: 0,
        courseGroupCount: 0,
        classCount: 0,
        majorCount: 0,
        collegeAdminRecord: {
            account: '',
            collegeAdminId: '',
            password: '',
            name: '',
            college: '',
            collegeId: ''
        }
    }
})

const getters = {
    getSystemMessage (state) {
        return state.systemMessage
    },
    getAdminInfo(state){
        return {token:state.token,adminId:state.adminId,collegeId:state.collegeId}
    },
    adminName(state){
        return state.adminName
    },
    adminPassword(state){
        return state.systemMessage.collegeAdminRecord.password
    },
    token(state){
        return state.token
    },
    adminId(state){
        return state.systemMessage.collegeAdminRecord.collegeAdminId
    }
}

const mutations = { // mutations改变state中的数据
    SET_TOKEN: (state, token) => { // 存储token
        state.token = token
    },
    SET_ADMIN_ID: (state, id) => {
        state.adminId = id
    },
    SET_ADMIN_NAME: (state, name)=>{
      state.adminName=name
    },
    SET_COLLEGE: (state ,admin) =>{
        state.collegeId=admin.collegeId
        state.collegeName=admin.collegeName
    },
    SET_SYSTEM_MESSAGE: (state, systemMessage) => {
        state.systemMessage = systemMessage
    },
    SET_IDENTITY: (sate, data) => {
        state.identity = data
    },
    CLEAR_TOKEN: (state) => {
        state.token = ''
    },
    NULL:()=>{}
}

const actions = { // 发送请求
    // commit 是调用上面mutations中函数的，state是上面的state
    // 调用登录接口
    login({ commit }, userInfo){
        const { account, password,identity } = userInfo
        // 设置用户身份
        Cookie.set('identity', userInfo.identity, {
            expires: 1/24
        }) // 设置时间为1小时
        return new Promise((resolve, reject) => {
            login({ username: account.trim(), password: password ,identity:identity}).then( data => {
                if(data.code === 1){ // 登录成功
                    commit('SET_TOKEN', data.data.token)
                    commit('SET_ADMIN_ID', data.data.adminId)
                    commit('SET_COLLEGE',{collegeId:data.data.collegeId,collegeName:data.data.collegeName})
                    commit('SET_ADMIN_NAME',data.data.name)
                    // 把数据存储到cookie中
                    Cookie.set('token',data.data.token, {
                        expires: 1/24
                    }) // 设置时间为1小时
                    Cookie.set('collegeId',data.data.collegeId, {
                        expires: 1/24
                    }) // 设置时间为1小时
                }
                resolve(data)
            }).catch(error=>{
                reject(error)
            })
        })
    },

    // 获取管理员信息
    updateSystemMessage({ commit}, adminInfo) {
        return new Promise((resolve, reject) => {
            systemMessage(adminInfo).then(res=>{
                //console.log(res)
                if (res.code === 1){
                    commit('SET_SYSTEM_MESSAGE',res.data)
                }
                resolve(res)
            }).catch(error=>{
                reject(error)
            })
        })


    },

    // 管理员修改密码
    changePasswd({ commit },data){
        return new Promise((resolve, reject) => {
            changePassword(data).then(res => {
               commit("NULL")
                resolve(res.msg)
            }).catch(error=>{
                reject(error)
            })
        })
    }
}


export default {
    namespaced: true,
    state,
    mutations,
    actions,
    getters
}
